import ch.kerbtier.malurus.*;
import ch.kerbtier.malurus.models.*;
import ch.kerbtier.malurus.components.*;

response.setContentType("text/html; charset=utf-8");

buiInstance = session.getAttribute("uiTree");

class Mir implements ActionListener{
	void execute(Event event){
		println("blabladssa dsadsad asdsad sad sad");
	}
}

if(buiInstance == null){
	root = new Rows();
	
	def selModel = new GenericModel<String>();
	selModel.setValue("bbb");
	
	
	l1 = Label.create("asdsda");
	l2 = Label.create("fdsf");
	l3 = Label.create(selModel);
	l4 = Label.create("saddsad");
	
	l5 = new SingleSelect();
	def model = new GenericListModel<String>();
	model.add("aaa");
	model.add("bbb");
	model.add("ccc");

	l5.setModel(model);
	l5.setSelectionModel(selModel);

	l6 = new Button();
	l6.setLabel("killem");
	l6.addOnClickListener(new Mir());
	
	root.add(new Rows().add(l1));
	root.add(new Rows().add(l2).add(l3));
	root.add(new Rows().add(l4));
	root.add(l5);
	root.add(l6);

	session.setAttribute("uiTree", new ch.kerbtier.malurus.coreimpl.CiMalurusInstance());
	buiInstance = session.getAttribute("uiTree");
	buiInstance.setRoot(root);
}

if(request.getParameter("buiLoad") || request.getParameter("buiRoot")){
	renderer = new ch.kerbtier.malurus.renderers.ChainRenderProvider();
	renderer.add(new ch.kerbtier.malurus.components.defaultrenderers.JavaDefaultRenderProvider());
	
	updater = new ch.kerbtier.malurus.updaters.ChainUpdateProvider();
	updater.add(new ch.kerbtier.malurus.components.defaultrenderers.JavaDefaultUpdateProvider());
	
	requestContext = new ch.kerbtier.malurus.coreimpl.CiRequestContext(request, response);
	requestContext.setBaseRenderProvider(renderer);
	
	writer = response.getWriter();
	
	if(request.getParameter("buiRoot")){
		writer.write("<BUI " + buiInstance.root.clientId + ">");
		def r = renderer.getRenderer(buiInstance.root.class.name, buiInstance.root.realisation);
		if(r != null){
			r.render(buiInstance.getRoot(), requestContext);
		}
		writer.write("</BUI>");		
	}else{
		buiInstance.visit(new ch.kerbtier.malurus.operations.UpdateVisitor(updater, requestContext));
		changes = buiInstance.visit(new ch.kerbtier.malurus.operations.CollectChangesVisitor()).getCollectedComponents();
		for(Component c:changes){
			writer.write("<BUI " + c.clientId + ">");		
			def r = renderer.getRenderer(c.class.name, c.realisation);
			if(r != null){
				r.render(c, requestContext);
			}
			writer.write("</BUI>");		
		}
	}
	buiInstance.visit(new ch.kerbtier.malurus.operations.CiResetVisitor());
	
}else{
	response.getWriter().write("""
<html>
	<head>
		<title>Malurus</title>
		<script type="text/javascript" src="res/prototype.js"></script>
		<script type="text/javascript" src="res/bui.js"></script>
		<script type="text/javascript">
			window.bui = new Bui();
		</script>
	</head>
	<body>
		<form id="rootForm" name="rootForm">
			
			<div id="root"></div>
		</form>
		<script type="text/javascript">
			window.bui.loadRoot();
		</script>

		<a href="javascript:window.bui.load()">poll</a>
	</body>
</html>
""");
}